package homework.Thirty;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonEvent implements ActionListener {
    Label NumDisp;
    int num;
    Button btReset;
    Button btStart;

    public ButtonEvent(){
        Frame frame=new Frame("测试窗口");
        frame.setSize(new Dimension(640,640));
        frame.setLayout(new BorderLayout());
        Panel panel=new Panel();
        panel.setLayout(new GridLayout(1,2));
        NumDisp=new Label(" 0 ",Label.CENTER);
        num=0;
        btReset=new Button("Reset");
        btStart=new Button("Start");
        btReset.addActionListener(this);
        btStart.addActionListener(this);
        btReset.setEnabled(false);
        panel.add(btReset);
        panel.add(btStart);
        frame.add("South",panel);
        frame.validate();
        btStart.requestFocus();
        NumDisp.setFont(new Font("Courier",Font.BOLD,60));
        frame.add("Center",NumDisp);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new ButtonEvent();
    }

    @Override
    public void actionPerformed(ActionEvent e){
        String arg=e.getActionCommand();
        if ("Reset".equals(arg)){
            NumDisp.setText("0");
            num=0;
            btReset.setEnabled(false);
            btStart.setEnabled(true);
        }else if ("Start".equals(arg)){
            btReset.setEnabled(true);
            btStart.setEnabled(false);
            btStart.invalidate();
            this.numstart();
        }
    }

    public void numstart(){
        for (int i = 0; i < 100; i++) {
            NumDisp.setText(Integer.toString(num));
            num++;
            try {
                Thread.sleep(100);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


}
